#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>

int main(int argc,char *argv[]) {
	if(strncmp(argv[argc-1],">",1) == 0) {
		printf("1\n");
		fflush(stdout);
		char *path = malloc(strlen(argv[argc-1]));
		strcpy(path,argv[argc-1]);
		int i;
		/*for(i=0;i<strlen(argv[argc-1])-1;i++) {
			*(path+i) = *(argv[argc-1]+i+1);
		}*/
		int fd = open(strtok(path,">"),O_CREAT | O_WRONLY | O_APPEND,0666);
		if(fd < 0) {
			printf("can't find file :%s\n",path);
			return 0;
		}
		//lseek(fd,0,SEEK_END);
		for(i=1;i<argc-1;i++) {
			write(fd,argv[i],strlen(argv[i]));
		}
		close(fd);
		return 0;
	}
	else {
		int i;
		for(i=1;i<argc;i++) {
			printf("%s ",argv[i]);
		}
		printf("\n");
	}
	return 0;
}
